﻿(function(){

    function getLayerNameByIndex( idx ) {
        var ref = new ActionReference();
        ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm  " ));
        ref.putIndex( charIDToTypeID( "Lyr " ), idx );
        return executeActionGet(ref).getString(charIDToTypeID( "Nm  " ));;
    }
    function getSelectedLayersIdx(){
        var selectedLayers = new Array;
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
        var desc = executeActionGet(ref);
        if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
            desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
            var c = desc.count
            var selectedLayers = new Array();
            for(var i=0;i<c;i++){
                try{
                    activeDocument.backgroundLayer;
                    selectedLayers.push(  desc.getReference( i ).getIndex() );
                }catch(e){
                    selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
                }
            }
        }else{
            var ref = new ActionReference();
            ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
            ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
            try{
                activeDocument.backgroundLayer;
                selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
            }catch(e){
                selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
            }
        }
        return selectedLayers;
    }

    var dom = app.activeDocument;
    var layers = dom.layers;
//    var layers = dom.artLayers;
    alert(getSelectedLayersIdx())
    var orgv = [];

    var i = 0;
    var len = layers.length;
    var l;

    for(i = 0; i < len; i++){
        l = layers[i];
        orgv.push(l.visible);               alert(i + "," + l.name)
        l.visible = false;
    }

//    for(i = 0; i < len; i ++){
//        l = layers[i];
//        l.visible = orgv[i];
//    }
})();
